package com.moxiao.yfjh.module.blind.service.memberMessage;

import com.moxiao.yfjh.module.blind.controller.admin.memberMessage.vo.MemberMessageCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.memberMessage.vo.MemberMessageExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.memberMessage.vo.MemberMessagePageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.memberMessage.vo.MemberMessageUpdateReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.blind.controller.admin.memberMessage.vo.*;
import com.moxiao.yfjh.module.blind.dal.dataobject.memberMessage.MemberMessageDO;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import com.moxiao.yfjh.module.blind.convert.memberMessage.MemberMessageConvert;
import com.moxiao.yfjh.module.blind.dal.mysql.memberMessage.MemberMessageMapper;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲聊天记录 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class MemberMessageServiceImpl implements MemberMessageService {

    @Resource
    private MemberMessageMapper memberMessageMapper;

    @Override
    public Integer createMemberMessage(MemberMessageCreateReqVO createReqVO) {
        // 插入
        MemberMessageDO memberMessage = MemberMessageConvert.INSTANCE.convert(createReqVO);
        memberMessageMapper.insert(memberMessage);
        // 返回
        return memberMessage.getMessageId();
    }

    @Override
    public void updateMemberMessage(MemberMessageUpdateReqVO updateReqVO) {
        // 校验存在
        validateMemberMessageExists(updateReqVO.getMessageId());
        // 更新
        MemberMessageDO updateObj = MemberMessageConvert.INSTANCE.convert(updateReqVO);
        memberMessageMapper.updateById(updateObj);
    }

    @Override
    public void deleteMemberMessage(Integer id) {
        // 校验存在
        validateMemberMessageExists(id);
        // 删除
        memberMessageMapper.deleteById(id);
    }

    private void validateMemberMessageExists(Integer id) {
        if (memberMessageMapper.selectById(id) == null) {
            throw exception(MEMBER_MESSAGE_NOT_EXISTS);
        }
    }

    @Override
    public MemberMessageDO getMemberMessage(Integer id) {
        return memberMessageMapper.selectById(id);
    }

    @Override
    public List<MemberMessageDO> getMemberMessageList(Collection<Integer> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return memberMessageMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MemberMessageDO> getMemberMessagePage(MemberMessagePageReqVO pageReqVO) {
        return memberMessageMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MemberMessageDO> getMemberMessageList(MemberMessageExportReqVO exportReqVO) {
        return memberMessageMapper.selectList(exportReqVO);
    }

    @Override
    public MemberMessageDO getMemberMessagePageByUserId(MemberMessagePageReqVO pageReqVO) {
//        pageReqVO.setMemberId(pageReqVO.getUserId());
        return memberMessageMapper.selectPageByUserId(pageReqVO);
    }

}
